package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Course;
import com.example.demo.entity.News;
import com.example.demo.entity.Question;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @descreption:
 * @author: xhr
 * @date: 2022/08/18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper userMapper;
    @Resource
    private IUserService userService;

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (res == null) {
            userMapper.insert(user);
            return Result.success();
        } else {
            return Result.error("-1", "名已经被占用！");
        }
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res == null) {
            return Result.error("-1", "名或密码错误！");
        }

        return Result.success(res);
    }

    @PostMapping
    public Result<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody User user) {
        User preUser = userMapper.selectById(user.getId());
        String a=new String( preUser.getUsername());
        String b=new String( user.getUsername());
        if (a.equals(b)) {
            userMapper.updateById(user);
            return Result.success();
        } else {
            List<User> res = userMapper.selectList(new QueryWrapper<User>().eq("username", user.getUsername()));
            if (res.size() == 0) {
                userMapper.updateById(user);
                return Result.success();
            } else {
                return Result.error("-1", "名已经被占用！");
            }
        }

    }


    @GetMapping("/{id}")
    public Result<?> getByID(@PathVariable Long id) {
        return Result.success(userMapper.selectById(id));
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        userMapper.deleteById(id);
        return Result.success();
    }

    //全查（给其他vue用）
    @GetMapping
    public Result<?> findAll() {
        return Result.success(userService.list());
    }

    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(defaultValue = "") String search) {
//        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like("username", search);
        }
        return Result.success(userMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }
}